package com.cn.shiro.zy.shiro.controller;

import com.alibaba.fastjson.JSON;
import com.cn.shiro.zy.shiro.entity.User;
import com.cn.shiro.zy.shiro.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangyi
 * @date 2018/12/11 21:25
 */
@Controller
public class UserController extends BaseController{

    @Value("${zy.salt}")
    private String salt;

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    @RequestMapping("/index")
    public String index(Model model){
        System.out.println("纳尼！！");
        return "index";
    }


    @GetMapping("/add")
    @RequiresPermissions(value = "user:add")
    public String add(Model model){
        model.addAttribute("msg","您未登录，无法添加信息");
        return "add";
    }

    @GetMapping("/update")
    @RequiresPermissions(value = "user:update")
    public String update(Model model){
        model.addAttribute("msg","您未登录，无法修改信息");
        return "update";
    }

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @PostMapping("/toLogin")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        Model model){
        System.out.println(userName+"---"+password);
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            model.addAttribute("msg","用户名和密码不能为空");
            return "login";
        }
        Subject subject = SecurityUtils.getSubject();
//        String string = MD5Util.encryptString(password+salt);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName,password);
        //根据前端数据判断时候 需要: 记住我
//        if(){usernamePasswordToken.setRememberMe(true);}
        try {
            subject.login(usernamePasswordToken);
            //登录成功
            return "redirect:/index";
        }catch (UnknownAccountException e){
            usernamePasswordToken.clear();
            model.addAttribute("msg","用户不存在!!!");
            return "login";
        }catch (IncorrectCredentialsException e){
            usernamePasswordToken.clear();
            model.addAttribute("msg","用户名或密码错误！！");
            return "login";
        }catch (ExcessiveAttemptsException e){
            model.addAttribute("msg","您的密码尝试过多！！");
            return "login";
        }
    }

    @GetMapping("/non")
    public String non(){
        return "nonauth";
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public String info(Model model){
        String string = JSON.toJSONString(getUser());
        model.addAttribute("userInfo",string);
        return "info";
    }


}
